//
//  CompassViewController.m
//  QuickCure1
//
//  Created by 李江波 on 2016/11/4.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "CompassViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "ProgressManager.h"

@interface CompassViewController ()<CLLocationManagerDelegate>

@property(nonatomic ,strong) UIImageView *img;

@property(nonatomic ,strong) CLLocationManager *manager;
@end

@implementation CompassViewController

-(UIImageView *)img{

    if (nil == _img) {
        
        _img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
        
        _img.center = self.view.center;
        
        _img.image = [UIImage imageNamed:@"Snip20161102_2"];
        _img.layer.cornerRadius = 45.0;
        _img.clipsToBounds = YES;
        
        [self.view addSubview:_img];
    }

    return _img;
}

-(CLLocationManager *)manager{

    if (nil == _manager) {
        
        _manager = [[CLLocationManager alloc]init];
        
        _manager.delegate = self;
        
//        [_manager requestAlwaysAuthorization];
    }
    return _manager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *lbl = [[UILabel alloc]init];
    
    lbl.text = @"黑马指南针，指领你走向越来越大的坑~🙃";
    
    [lbl sizeToFit];
    
    [self.view addSubview:lbl];
    
    [lbl mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(100);
        
    }];
    
    
    //使用位置传感器，朝向
    //磁力计
    if ([CLLocationManager headingAvailable]) {
        
        [self.manager startUpdatingHeading];
    }else{
    
        [ProgressManager alertErrorStatus:@"磁力计坏了"];
    }
    
    
}

-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{

    CGFloat angle = newHeading.magneticHeading;
    
    CGFloat hudu = angle /180 *M_PI ;
    
    [UIView animateWithDuration:0.25 animations:^{
       
        self.img.transform = CGAffineTransformMakeRotation(-hudu);
        
    }];
    
    

}


@end
